<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Article extends Model
{
    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'addtime_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $row->save(['weigh' => $row['id']]);
        });
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'),'0' => __('Status 0')];
    }


    public function getSiteIdList()
    {
//        return ['1' => __('Module_type 1'),'2' => __('Module_type 2'),'3' => __('Module_type 3'),'99' => __('Module_type 99')];
        $result = array();
        $list = Db::name('article_cate')->field('id,name')->select();
        foreach($list as $key=>$val){
            $result[$val['id']] = $val['name'];
        }
        return $result;

    }

    public function getSiteIdTextAttr($value, $data)
    {
        $value = $value ? $value : $data['site_id'];
        $list = $this->getSiteIdList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    public function getAddtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['addtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAddtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function ArticleCate()
    {
        return $this->belongsTo('ArticleCate', 'site_id','id')->setEagerlyType(0);
    }



}
